/* 
 * @Title: MerchantNoticeController.java 
 * @Package com.jiankangjin.weixin.controller 
 * @Description: TODO 
 * @author neason
 * @date 2014年7月16日 上午11:09:42 
 * @version V1.0   
 */ 
package com.jiankangjin.pay.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.jiankangjin.httpclient.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.encryptKit.EncryptUtil;
import com.jiankangjin.encryptKit.SignUtil;
import com.jiankangjin.pay.object.Merchant;
import com.jiankangjin.pay.object.Order;
import com.jiankangjin.pay.services.MerchantNoticeServices;
import com.jiankangjin.pay.services.UpdateOrderServieces;
import com.jiankangjin.pay.util.XMLDoc;

/**  
 * 类功能说明 
 * 类修改者 修改日期 
 * 修改说明  
 * <p>Title: MerchantNoticeController.java</p>
 * <p>Copyright: Copyright (c) 2014</p>
 * @author neason 
 * @date 2014年7月16日 上午11:09:42 
 * @version V1.0 
 */
@Controller
public class MerchantNoticeController {
	@Autowired
	private MerchantNoticeServices merchantNoticeServices;
	
	@Autowired
	private UpdateOrderServieces updateOrderServieces;
	/**
	 * 
	 * 通知 
	 * neason  2014年7月16日 
	 * 修改者名字 修改日期 
	 * 修改内容  
	 * @param @param signtype  签名方式
	 * @param @param serviceversion 接口版本
	 * @param @param inputcharset 字符集
	 * @param @param sign 签名
	 * @param @param signkeyindex 密钥序号
	 * @param @param trademode 交易模式
	 * @param @param tradestate 交易状态
	 * @param @param payinfo 支付结果信息
	 * @param @param partner 商户号 
	 * @param @param banktype 付款银行
	 * @param @param bankbillno 银行订单号
	 * @param @param totalfee 总金额
	 * @param @param feetype 币种
	 * @param @param notifyid 通知 ID
	 * @param @param transactionid 订单号 
	 * @param @param outtradeno 商户订单号
	 * @param @param attach 商户数据包
	 * @param @param timeend  支付完成时间
	 * @param @param transportfee 物流费用
	 * @param @param productfee 物品费用
	 * @param @param discount 折扣价格 
	 * @param @param buyeralias 买家别名
	 * @param @return     
	 * @return ModelAndView    
	 * @throws  Exception
	 */
	@RequestMapping(value="/notice",method=RequestMethod.POST)
	@ResponseBody
	public  String notice(
			@RequestParam(required = false) String signtype,
			@RequestParam(required = false) String serviceversion,
			@RequestParam(required = false) String inputcharset,
			@RequestParam(required = false) String sign,
			@RequestParam(required = false) Integer signkeyindex,
			@RequestParam(required = false) Integer trademode,
			@RequestParam(required = false) Integer tradestate,
			@RequestParam(required = false) String payinfo,
			@RequestParam(required = false) String partner,
			@RequestParam(required = false) String banktype,
			@RequestParam(required = false) String bankbillno,
			@RequestParam(required = false) Integer totalfee,
			@RequestParam(required = false) Integer feetype,
			@RequestParam(required = false) String notifyid,
			@RequestParam(required = false) String transactionid,
			@RequestParam(required = false) String outtradeno,
			@RequestParam(required = false) String attach,
			@RequestParam(required = false) String timeend,
			@RequestParam(required = false) Integer transportfee,
			@RequestParam(required = false) Integer productfee,
			@RequestParam(required = false) Integer discount,
			@RequestParam(required = false) String buyeralias,
			@RequestBody String xml
			){
		try { 
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("signtype", signtype);
			map.put("serviceversion", serviceversion);
			map.put("inputcharset", inputcharset);
			map.put("signkeyindex", signkeyindex);
			map.put("trademode", trademode);
			map.put("tradestate", tradestate);
			map.put("payinfo", payinfo);
			map.put("partner", partner);
			map.put("banktype", banktype);
			map.put("bankbillno", bankbillno);
			map.put("totalfee", totalfee);
			map.put("feetype", feetype);
			map.put("notifyid", notifyid);
			map.put("transactionid", transactionid);
			map.put("outtradeno", outtradeno);
			map.put("attach", attach);
			map.put("timeend", timeend);
			map.put("transportfee", transportfee);
			map.put("productfee", productfee);
			map.put("discount", discount);
			map.put("buyeralias", buyeralias);
			Merchant merchant=new Merchant();
			String signKey = merchant.getMerchantSummaryKey();
	        String privateKey = merchant.getMerchantPrivateDecrypt();
	        String str1 = SignUtil.sort(map, "&");
	        str1 = str1 + "&key=" + signKey;
	        String signStr = EncryptUtil.sha(str1);
			
			 Order order=new Order();
			 //通过商户订单号查询订单状态
			 order = merchantNoticeServices.orderStatus(outtradeno);
			 System.out.println(order.getOrderStatus());
			//取得商户订单支付信息
			if(order.getOrderStatus()=="00"){
				Map<String, Object> map1=new HashMap<String, Object>();
				map1.put("tradestate", tradestate);
				map1.put("payinfo", payinfo);
				map1.put("banktype", banktype);
				map1.put("notifyid", notifyid);
				map1.put("attach", attach);
				map1.put("buyeralias", buyeralias);
				Map<String, String> map3=new HashMap<String, String>();
				XMLDoc doc = new XMLDoc();
				map3=doc.xmlElements(xml);
				Set<String> set=map3.keySet();
				for(String st:set){
					map1.put(st, map3.get(st));
				}
				//修改数据入库
				updateOrderServieces.update(map1);
				
				Map<String, Object> map2=new HashMap<String, Object>();
				map2.put("pid",order.getMerchantNumber());
				map2.put("transactionid",order.getMerchantOrderId());
				map2.put("orderamount", order.getOrderMoney());
				map2.put("dealid", order.getOrderId());
				map2.put("channeltype", order.getChannelType());
				//渠道交易号
				map2.put("channeldealid", order.getChannelUserId());
				map2.put("dealtime", order.getOrderUpdateTime());
				map2.put("ext1", order.getOrderRemark());
				map2.put("ext2", order.getOrderRemarkTwo());
				map2.put("payresult", order.getOrderStatus());
				//处理结果描述
				map2.put("resultdesc", "支付成功");
				//签名
				map2.put("signmsg",signStr);
				HttpClient httpClient = HttpClient.getInstance();
				//回调商户url
//				String sign1=httpClient.doPOSTgetJSONstr(order.getOrderCallback(), (HashMap<String, Object>) map2);
				//处理返回结果
//				if (sign1=="success") {
//				}
			}
			return "success";
		}catch(Exception e){
			e.printStackTrace();
			return "fail";
		}
	}
}
 